﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;

public partial class CardCenter_ChargeAccount : System.Web.UI.Page
{
    AccountsCharge bllAccount = new AccountsCharge();
    WebUtility utility = new WebUtility();
    UserBean bllUser = new UserBean();

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn_ChargeAccount_Click(object sender, ImageClickEventArgs e)
    {
        string mobileNO = this.txt_AccountNO.Text.Trim();
        string cardNO = this.txt_CardNO.Text.Trim();
        string cardPwd = this.txt_CardPwd.Text.Trim();

        bool flag = bllUser.IsExistsUserInfo(mobileNO);

        if (flag == false)
        {
            utility.AlertMsg("该帐户不存在，请重新输入！");
            this.txt_AccountNO.Text = string.Empty;
            this.txt_CardNO.Text = string.Empty;
            this.txt_CardPwd.Text = string.Empty;
            return;
        }

        DataTable dtCardInfo = bllAccount.CheckCardInfo(cardNO, cardPwd);
        if (dtCardInfo.Rows.Count == 0)
        {
            utility.AlertMsg("卡号或密码错误，请重新输入！");
            this.txt_AccountNO.Text = string.Empty;
            this.txt_CardNO.Text = string.Empty;
            this.txt_CardPwd.Text = string.Empty;
            return;
        }
        else if (dtCardInfo.Rows[0]["CPXX_CPLB"].ToString() == "实体赠卡" || dtCardInfo.Rows[0]["CPXX_CPLB"].ToString() == "虚拟赠卡")
        {
            utility.AlertMsg("对不起，您输入的卡号系赠卡，不能进行充值操作，请输入其他卡号！");
            this.txt_AccountNO.Text = string.Empty;
            this.txt_CardNO.Text = string.Empty;
            this.txt_CardPwd.Text = string.Empty;
            return;
        }
        else if (Convert.ToBoolean(dtCardInfo.Rows[0]["CPXX_IsUseable"].ToString()) == false ||
            Convert.ToBoolean(dtCardInfo.Rows[0]["CPXX_ISActive"].ToString()) == false ||
            Convert.ToBoolean(dtCardInfo.Rows[0]["CPXX_IsScraped"].ToString()) == false)
        {
            utility.AlertMsg("对不起，您输入的卡号不能使用！");
            this.txt_AccountNO.Text = string.Empty;
            this.txt_CardNO.Text = string.Empty;
            this.txt_CardPwd.Text = string.Empty;
            return;
        }
        else if (Convert.ToDateTime(dtCardInfo.Rows[0]["CPXX_ExpiredTime"].ToString()) < Convert.ToDateTime(DateTime.Now.ToString()))
        {
            utility.AlertMsg("对不起，您输入的卡号有效期已过！");
            this.txt_AccountNO.Text = string.Empty;
            this.txt_CardNO.Text = string.Empty;
            this.txt_CardPwd.Text = string.Empty;
            return;
        }
        else
        {
            string cardPoints = bllAccount.GetPointsValue(cardNO);

            int count1 = (int)bllAccount.EditCardInfo(cardNO, cardPwd);
            int count2 = (int)bllAccount.EditChargeAccount(mobileNO, cardPoints, "为注册帐户充值");
            int count3 = (int)bllAccount.EditAccountInfo(cardNO, mobileNO, cardPoints);
            int count4 = (int)bllAccount.EditCardPoints(cardNO);

            if (count1 > 0 && count2 > 0 && count3 > 0 && count4 > 0)
            {
                this.txt_AccountNO.Text = string.Empty;
                this.txt_CardNO.Text = string.Empty;
                this.txt_CardPwd.Text = string.Empty;
                Response.Redirect("ChargeSuccess.aspx");
            }
            else
            {
                this.txt_AccountNO.Text = string.Empty;
                this.txt_CardNO.Text = string.Empty;
                this.txt_CardPwd.Text = string.Empty;
                Response.Redirect("ChargeFailed.aspx");
            }
        }
        //charge end

    }
}
